package com.hotchpotch.activity;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;

import com.hotchpotch.R;
import com.hotchpotch.adapter.ViewPagerStateAdapter;
import com.hotchpotch.base.BaseActivity;
import com.hotchpotch.custom.CustomViewPager;
import com.hotchpotch.fragment.TestFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class TabLayoutAct extends BaseActivity {
    private static final String TAG = "TabLayoutAct";
    @BindView(R.id.main_tab)
    TabLayout tabLayout;
    @BindView(R.id.viewpager)
    CustomViewPager viewPager;
    private ViewPagerStateAdapter viewPagerStateAdapter;
    private List<Fragment> fragmentList = new ArrayList<>();
    private TestFragment fragment1 = TestFragment.newInstance("这是首页");
    private TestFragment fragment2 = TestFragment.newInstance("这是分类页");
    private TestFragment fragment3 = TestFragment.newInstance("这是购物车");
    private TestFragment fragment4 = TestFragment.newInstance("这是个人中心");

    @Override
    protected int getLayoutId() {
        return R.layout.act_tablayout;
    }

    @Override
    protected void initView() {
        initViewPager();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvents() {

    }

    private void initViewPager() {
        viewPager.setCanScroll(true);
        viewPager.setOffscreenPageLimit(4);
        fragmentList.add(fragment1);
        fragmentList.add(fragment2);
        fragmentList.add(fragment3);
        fragmentList.add(fragment4);
        viewPagerStateAdapter = new ViewPagerStateAdapter(getSupportFragmentManager(), fragmentList);
        viewPager.setAdapter(viewPagerStateAdapter);

        tabLayout.setupWithViewPager(viewPager);
//        tabLayout.setTabsFromPagerAdapter(viewPagerStateAdapter);//23.1.1可用
        tabLayout.getTabAt(0).setText("首页").setIcon(R.drawable.selector_tablayout_one);
        tabLayout.getTabAt(1).setText("分类").setIcon(R.drawable.selector_tablayout_two);
        tabLayout.getTabAt(2).setText("购物车").setIcon(R.drawable.selector_tablayout_three);
        tabLayout.getTabAt(3).setText("个人中心").setIcon(R.drawable.selector_tablayout_four);
    }
}
